<template>
	<Container>
		<view class="calc-index">
			<view class="calc-total">
				<view class="total">
					{{money(this.params.money * (params.priceUnit === 'wan' ? 10000 : 1) + termList[termList.length - 1].paidInterest)}}
				</view>
				<view class="total_text">还款总额(元)</view>
				<view class="other-money">
					<view class="other-item">
						<view class="num">{{money(termList[0].installment)}}</view>
						<view class="lab">首月还款金额(元)</view>
					</view>
					<view class="other-item">
						<view class="num">{{money(termList[termList.length - 1].paidInterest)}}</view>
						<view class="lab">总利息(元)</view>
					</view>
				</view>
			</view>

			<view class="info">
				<view class="info-item">
					<view class="lab">贷款金额</view>
					<view class="val">{{params.money}}{{moneyUnit[params.priceUnit]}}</view>
				</view>
				<view class="info-item">
					<view class="lab">贷款期限</view>
					<view class="val">{{params.term}}{{m_y[params.termUnit]}}</view>
				</view>
				<view class="info-item">
					<view class="lab">贷款利率</view>
					<view class="val">{{params.rate}}%/{{m_y[params.yOrmUnit]}}</view>
				</view>
				<view class="info-item">
					<view class="lab">还款方式</view>
					<view class="val">{{params.repayment}}</view>
				</view>
			</view>

			<view class="list-box">
				<view class="list">
					<view class="header">
						<view class="lab">期</view>
						<view class="lab">月供额(元)</view>
						<view class="lab">月供本金(元)</view>
						<view class="lab">月供利息(元)</view>
						<view class="lab">已还利息(元)</view>
						<view class="lab">剩余待还(元)</view>
					</view>
					<view class="item" v-for="(term, index) in termList" :key="index">
						<view class="lab">{{index + 1}}</view>
						<view class="lab">{{money(term.installment)}}</view>
						<view class="lab">{{money(term.principal)}}</view>
						<view class="lab">{{money(term.interest)}}</view>
						<view class="lab">{{money(term.paidInterest)}}</view>
						<view class="lab">{{money(term.balance)}}</view>
					</view>
				</view>
			</view>

		</view>
	</Container>
</template>

<script>
	export default {
		data() {
			return {
				m_y: {
					'year': '年',
					'month': '月'
				},
				moneyUnit: {
					wan: '万',
					yuan: '元'
				},
				params: {},
				// 月供额 - Installment
				// 月供本金 - Principal
				// 月供利息 - Interest
				// 已还利息 - Paid interest
				// 剩余待还 - Balance
				termList: []
			};
		},
		onLoad(options) {
			this.params = JSON.parse(options.query)
			console.log(this.params)
			this.termList = this[this.params.repaymentType](this.params.money * (this.params.priceUnit === 'wan' ? 10000 :
				1), this.params.term * (this.params.termUnit === 'year' ? 12 : 1), (this.params.yOrmUnit ===
				'year' ? this.params.rate : this.params.rate * 12) / 100)
				console.log(this.termList)
		},
		methods: {
			/** 
			 * 等额本金计算方法
				amount: 总贷款
				months: 贷款期数
				rate: 贷款年利率
			 **/
			DEBJ(amount, months, rate) {
				const principalPerMonth = amount / months;
				const repaymentSchedule = [];

				let balance = amount;
				let paidInterest = 0;

				for (let i = 0; i < months; i++) {
					const interest = balance * rate / 12;
					const installment = principalPerMonth + interest;
					paidInterest += interest;

					repaymentSchedule.push({
						installment,
						principal: principalPerMonth,
						interest,
						paidInterest,
						balance: balance - principalPerMonth
					});

					balance -= principalPerMonth;
				}

				return repaymentSchedule;
			},
			/**
			 * 等额本息计算方法
				amount: 总贷款
				months: 贷款期数
				rate: 贷款年利率
			 **/
			DEBX(amount, months, rate) {
				const installment = amount * rate * Math.pow(1 + rate, months) / (Math.pow(1 + rate, months) - 1);
				const repaymentSchedule = [];

				let balance = amount;
				let paidInterest = 0;

				for (let i = 0; i < months; i++) {
					const interest = balance * (rate/12);
					paidInterest += interest;

					let principal = installment - interest;
					if (principal > balance) {
						principal = balance;
					}

					repaymentSchedule.push({
						installment,
						principal,
						interest,
						paidInterest,
						balance: balance - principal
					});

					balance -= principal;
				}

				return repaymentSchedule;
			},
			/**
			 * 先息后本计算方法
				amount: 总贷款
				months: 贷款期数
				rate: 贷款年利率
			 **/
			XXHB(amount, months, rate) {
				console.log(amount, months, rate)
				const repaymentSchedule = [];

				let balance = amount;
				let paidInterest = 0;

				for (let i = 0; i < months; i++) {
					const interest = balance * (rate / 12);
					paidInterest += interest;

					let principal = 0;
					if (i === months - 1) {
						principal = balance;
					}

					repaymentSchedule.push({
						installment: interest + principal,
						principal,
						interest,
						paidInterest,
						balance: balance - principal
					});

					balance -= principal;
				}

				return repaymentSchedule;
			},
			/** 利随本清
				 amount: 总贷款
				 months: 贷款期数
				 rate: 贷款年利率
			 **/
			LSBQ(amount, months, rate) {
				const repaymentSchedule = [];

				let balance = amount;
				let paidInterest = 0;

				const payment = amount / months;

				for (let i = 0; i < months; i++) {
					const interest = balance * (rate / 12);
					paidInterest += interest;

					const principal = payment - interest;

					repaymentSchedule.push({
						installment: payment,
						principal,
						interest,
						paidInterest,
						balance: balance - principal
					});

					balance -= principal;
				}

				return repaymentSchedule;
			},
			/** 等本等息
				 amount: 总贷款
				 months: 贷款期数
				 rate: 贷款年利率
			 **/
			DBDX(amount, months, rate) {
				const repaymentSchedule = [];

				const principalPerMonth = amount / months;
				let balance = amount;
				let paidInterest = 0;

				for (let i = 0; i < months; i++) {
					const interest = balance * rate / 12;
					paidInterest += interest;

					repaymentSchedule.push({
						installment: principalPerMonth + interest,
						principal: principalPerMonth,
						interest,
						paidInterest,
						balance: balance - principalPerMonth
					});

					balance -= principalPerMonth;
				}

				return repaymentSchedule;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.calc-index {
		padding-bottom: calc(env(safe-area-inset-bottom) + 32rpx);

		.calc-total {
			min-height: 300rpx;
			background-color: #2C405A;
			padding: 24rpx 32rpx 100rpx;

			.total {
				font-size: 70rpx;
				font-weight: 600;
				font-family: 'Courier New', Courier, monospace;
				color: #fff;
				text-align: center;
				padding-top: 30rpx;
			}

			.total_text {
				font-size: 34rpx;
				color: #fff;
				opacity: 0.8;
				text-align: center;
			}

			.other-money {
				padding-top: 32rpx;
				display: flex;
				align-items: center;
				justify-content: space-around;

				.other-item {
					flex: 1;
					display: flex;
					flex-direction: column;

					.num {
						font-size: 40rpx;
						color: #fff;
						text-align: center;
					}

					.lab {
						font-size: 30rpx;
						color: #fff;
						opacity: 0.8;
						text-align: center;
					}
				}
			}
		}

		.info {
			width: calc(100vw - 64rpx);
			margin: auto;
			position: relative;
			top: -50rpx;
			box-shadow: 0 10rpx 40rpx -10rpx rgba(0, 64, 128, .2);
			border-radius: 16rpx;
			background-color: #fff;
			padding: 24rpx 32rpx;

			.info-item {
				height: 80rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: 2rpx solid #eee;

				&:last-child {
					border: none;
				}

				.val {
					font-weight: 600;
					font-size: 32rpx;
				}
			}
		}

		.list-box {
			width: 100vw;
			overflow-x: auto;
			padding-bottom: 32rpx;

			.list {
				width: 100%;

				.header {
					width: 100%;
					display: flex;
					height: 60rpx;

					.lab {
						background-color: #f5f6f7;
						flex: none;
						line-height: 60rpx;
						width: calc((100vw - 70rpx) / 3);
						text-align: center;

						&:nth-child(1) {
							width: 70rpx;
							text-align: center;
						}
					}
				}

				.item {
					width: 100%;
					display: flex;

					.lab {
						flex: none;
						width: calc((100vw - 70rpx) / 3);
						line-height: 60rpx;
						text-align: center;
						padding: 0 10rpx;

						&:nth-child(1) {
							width: 70rpx;
							text-align: center;
						}
					}
				}

				.item:nth-child(odd) {
					.lab {
						background-color: #f5f6f7;
					}
				}

			}
		}

	}
</style>